//
//  BSLatestViewController.m
//  BaiSiBuDeJie
//
//  Created by 阿卡 on 16/8/23.
//  Copyright © 2016年 tsaievan. All rights reserved.
//

#import "BSLatestViewController.h"
#import "HMModel.h"
#import "BSLatestCollectionViewCell.h"
#import "BSWebViewController.h"


@interface BSLatestViewController () <UICollectionViewDataSource,UICollectionViewDelegate>
/**
 *  频道ScrollView
 */
@property (weak, nonatomic) IBOutlet UIScrollView *LatestScrollView;
/**
 *  选中的Label
 */
@property (nonatomic ,strong) UILabel *selectedLab;
/**
 *  collectionView
 */
@property (weak, nonatomic) IBOutlet UICollectionView *latestCollectionView;
/**
 *  FlowLayout
 */
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *latestFlowLayout;
/**
 *  label的数组
 */
@property (nonatomic ,strong) NSArray *labArr;

//模型数据
@property(nonatomic,strong)NSArray *dataArr;



@end



static NSString *rid = @"Cell";

@implementation BSLatestViewController
/**
 *  懒加载
 */
-(NSArray *)dataArr
{
    if (_dataArr == nil) {
        
        NSArray *arr = [HMModel getDataArrWithName:@"main.plist"];
        _dataArr = arr;
    }
    return _dataArr;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
   
//    self.navigationItem.title = @"最新";
    
    //设置NavgationBar
    [self creatNavigationBar];
    //创建频道
    [self creatChanelLable];
    
    
    [self.latestCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:rid];
    
   
    
    
}



#pragma mark - creatNavigationBar

- (void)creatNavigationBar
{
    
    //navigationItem
    UIImageView *latestTitleImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    [self.navigationItem setTitleView:latestTitleImg];
    
    //左边审的Item
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"review_post_nav_iconN_20x17_"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
}



#pragma mark - 左边审按钮的点击事件

- (void)leftBarButtonItemAction
{
    NSLog(@"跳转到增加页面的审帖");
}



#pragma mark - 创建频道ScrollView

- (void)creatChanelLable
{
    NSArray *chanelArr = @[@"全部",@"视频",@"图片",@"段子",@"网红",@"美女",@"冷知识",@"游戏",@"声音"];
    
    NSMutableArray *mutArr = [NSMutableArray array];
    
    CGFloat labW = kScreenW / 6;
    CGFloat labH = self.LatestScrollView.bounds.size.height;
    
    for (int i = 0; i < chanelArr.count; i++)
    {
        UILabel *lab = [[UILabel alloc] init];
        
        lab.frame = CGRectMake(labW * i, 0, labW, labH);
        
        lab.text = chanelArr[i];
        
        [lab setTextColor:[UIColor grayColor]];
        lab.font = [UIFont systemFontOfSize:17];
        
        lab.textAlignment = NSTextAlignmentCenter;
        
        [self.LatestScrollView addSubview:lab];
        
        if (i == 0)
        {
            self.selectedLab = lab;
            
            [lab setTextColor:[UIColor redColor]];
            lab.font = [UIFont systemFontOfSize:20];
        }
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
        
        lab.tag = i;
        
        [lab addGestureRecognizer:tap];
        
        lab.userInteractionEnabled = YES;
        
        [mutArr addObject:lab];
        
    }
    
    self.LatestScrollView.contentSize = CGSizeMake(labW * chanelArr.count, 0);
    
    self.LatestScrollView.bounces = NO;
    
    self.LatestScrollView.showsHorizontalScrollIndicator = NO;
    
    self.labArr = mutArr.copy;
}



#pragma mark - 频道点击事件

- (void)tapClick:(UITapGestureRecognizer *)tap
{
    
    [self.selectedLab setTextColor:[UIColor grayColor]];
    self.selectedLab.font = [UIFont systemFontOfSize:17];
    
    UILabel *lab = (UILabel *)tap.view;
    
    [lab setTextColor:[UIColor redColor]];
    lab.font = [UIFont systemFontOfSize:20];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:lab.tag inSection:0];
    
    [self.latestCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:0 animated:YES];
    
    self.selectedLab = lab;
    
    // 计算选中的label居中时,要滚动的偏移量
    CGFloat labelOffsetX = lab.center.x - (self.view.bounds.size.width) * 0.5;
    //特殊情况处理
    //最小
    CGFloat minOffsetX = 0;
    //最大
    CGFloat maxOffsetX =  self.LatestScrollView.contentSize.width - self.view.bounds.size.width;
    
    // 判断偏移量的临界值
    if (labelOffsetX < minOffsetX)
    {
        labelOffsetX = 0;
    }
    else if (labelOffsetX > maxOffsetX)
    {
        labelOffsetX = maxOffsetX;
    }
    
    // 设置频道滚动视图的滚动
    [self.LatestScrollView setContentOffset:CGPointMake(labelOffsetX, 0) animated:YES];
}

#pragma mark - 联动 滚动collectionView的时候频道也跟着动

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / scrollView.bounds.size.width;
    
    UILabel *lab = self.labArr[index];
    
    // 计算选中的label居中时,要滚动的偏移量
    CGFloat labelOffsetX = lab.center.x - (self.view.bounds.size.width) * 0.5;
    //特殊情况处理
    //最小
    CGFloat minOffsetX = 0;
    //最大
    CGFloat maxOffsetX =  self.LatestScrollView.contentSize.width - self.view.bounds.size.width;
    
    // 判断偏移量的临界值
    if (labelOffsetX < minOffsetX)
    {
        labelOffsetX = 0;
    }
    else if (labelOffsetX > maxOffsetX)
    {
        labelOffsetX = maxOffsetX;
    }
    
    // 设置频道滚动视图的滚动
    [self.LatestScrollView setContentOffset:CGPointMake(labelOffsetX, 0) animated:YES];
    
    [self.selectedLab setTextColor:[UIColor grayColor]];
    self.selectedLab.font = [UIFont systemFontOfSize:17];
    
    [lab setTextColor:[UIColor redColor]];
    lab.font = [UIFont systemFontOfSize:20];
    self.selectedLab = lab;
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    self.latestFlowLayout.itemSize = self.latestCollectionView.bounds.size;
    
    
    
}

#pragma mark - CollectionView数据源协议

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BSLatestCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BSLatestCollectionViewCell" forIndexPath:indexPath];
    
    // Configure the cell
    cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1];
    HMModel *model = self.dataArr[indexPath.item];
    
    cell.url = model.channel_url;
    
//pushWebView
    [cell drumpToWebViewWithblock:^{
        BSWebViewController *webViewVc = [BSWebViewController new];
        [self.navigationController pushViewController:webViewVc animated:YES];
        //修改返回的按钮
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];;
    }];
    
    return cell;
    
    
}

@end
